package newflink4;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.SideOutputDataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.connectors.redis.RedisSink;
import org.apache.flink.streaming.connectors.redis.common.config.FlinkJedisPoolConfig;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class job1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);
        env.enableCheckpointing(5000);

        KafkaSource<String> source = KafkaSource.<String>builder()
                .setBootstrapServers("bigdata1:9092")
                .setTopics("order")
                .setGroupId("job1")
                .setStartingOffsets(OffsetsInitializer.earliest())
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();

        FlinkJedisPoolConfig config = new FlinkJedisPoolConfig.Builder()
                .setPort(6379)
                .setHost("bigdata1")
                .setPassword("123456")
                .build();

        WatermarkStrategy<String> watermarkStrategy = WatermarkStrategy.<String>noWatermarks();

        OutputTag<Integer> outputTag = new OutputTag<>("tuihuimoney", TypeInformation.of(Integer.class));

        SingleOutputStreamOperator<Integer> process = env.fromSource(source, watermarkStrategy, "job")
                .map(s -> s.split(","))
                .filter(s -> s[1].equals("1004") || s[1].equals("1006"))
                .process(new ProcessFunction<String[], Integer>() {


                    ValueState<Integer> money;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        money = getRuntimeContext().getState(new ValueStateDescriptor<>("money", Integer.TYPE));
                    }

                    @Override
                    public void processElement(String[] strings, ProcessFunction<String[], Integer>.Context context, Collector<Integer> collector) throws Exception {

                        Integer value = money.value();
                        if (strings[1].equals("1004")) {
                            money.update(value + Integer.valueOf(strings[2]));
                        } else {
                            context.output(outputTag , Integer.valueOf(strings[2]));
                            money.update(value - Integer.valueOf(strings[2]));
                        }
                        collector.collect(money.value());
                    }
                });

        SideOutputDataStream<Integer> sideOutput = process.getSideOutput(outputTag);
        sideOutput.addSink(new RedisSink<>(config , new newflink4.RedisSink()));
        process.addSink(new RedisSink<>(config , new newflink4.RedisSink()));

        env.execute();
    }
}
